package com.xxhui.screenshotdemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.xxhui.screenshotdemo.server.MyService;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button getScreenShot;
    private ImageView displayScreenShot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getScreenShot = (Button) findViewById(R.id.get_screenShot);
        displayScreenShot = (ImageView) findViewById(R.id.display_screenShot);
    }

    public void on(View view) {
        Point point = getScreenSize();
        int w = point.x;
        int h = point.y;
        Toast.makeText(this, "( " + w + " , " + h + " )", Toast.LENGTH_SHORT).show();
        Bitmap bitmap = screenShot(w, h);
        if (bitmap == null) {
            Log.e(TAG, "on: bitmap is null");
            MyService service = new MyService();
            Intent intent = new Intent(this,MyService.class);
            startService(intent);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //stopService(intent);
            return;
        }
//        Bitmap ss = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
//        Canvas c = new Canvas(ss);
//        c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
//        c.translate(-w / 2, -h / 2);
//        c.drawBitmap(bitmap, 0, 0, null);
        //c.setBitmap(null);
        //bitmap = ss;

        displayScreenShot.setImageBitmap(bitmap);

    }

    private Point getScreenSize() {
        WindowManager manager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getRealMetrics(metrics);
        Point point = new Point(metrics.widthPixels, metrics.heightPixels);
        return point;
    }

    private Bitmap screenShot(int screen_width, int screen_height) {
        Object bitmap = null;
        try {
            Class<?> surface = Class.forName("android.view.SurfaceControl");
            Class[] argsClass = new Class[2];
            argsClass[0] = int.class;
            argsClass[1] = int.class;
            Method screenshot = surface.getMethod("screenshot", int.class, int.class);
            Method method = surface.getMethod("screenshot", argsClass);
            screenshot.setAccessible(true);
            method.setAccessible(true);
            //bitmap = screenshot.invoke(surface, screen_width, screen_height);
            bitmap = method.invoke(null , new Object[]{screen_width ,screen_height});
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "screenShot: " + e.getMessage());
        }
        if (bitmap == null)
            Log.e(TAG, "screenShot: screenShot return null");
        else
            Log.e(TAG, "screenShot: screenShot return not null");
        return (Bitmap) bitmap;
    }


}
